package com.qen.conditions.query;

import com.qen.conditions.AbstractChainWrapper;
import com.qen.repository.BaseDao;
import com.qen.wrapper.query.QueryWrapper;
import com.qen.wrapper.query.interfaces.QueryBase;
import org.beetl.sql.clazz.ColDesc;
import org.beetl.sql.clazz.kit.BeetlSQLException;

import java.util.List;
import java.util.function.Predicate;

/**
 * @author licz
 * @data 2025年05月26日 8:52 AM
 */
public class QueryChainWrapper<T>
        extends AbstractChainWrapper<T, String, QueryChainWrapper<T>, QueryWrapper<T>>
        implements ChainQuery<T>, QueryBase<QueryChainWrapper<T>, T, String> {

    private final BaseDao<T> baseDao;
    private final Class<T> entityClass;

    public QueryChainWrapper(BaseDao<T> baseDao) {
        super();
        this.baseDao = baseDao;
        this.entityClass = null;
        super.wrapperChildren = new QueryWrapper<>();
    }

    public QueryChainWrapper(Class<T> entityClass) {
        super();
        this.baseDao = null;
        this.entityClass = entityClass;
        super.wrapperChildren = new QueryWrapper<>();
    }

    @Override
    public QueryChainWrapper<T> select(boolean condition, List<String> columns) {
        wrapperChildren.select(condition, columns);
        return typedThis;
    }

    @Override
    public QueryChainWrapper<T> select(Class<T> entityClass, Predicate<ColDesc> predicate) {
        wrapperChildren.select(entityClass, predicate);
        return typedThis;
    }

    @Override
    public String getSqlSelect() {
        throw new BeetlSQLException(BeetlSQLException.ERROR, String.format("can not use this method for \"%s\"", "getSqlSelect"));
    }

    @Override
    public BaseDao<T> getBaseDao() {
        return baseDao;
    }

    /**
     * 获取当前实体Class
     *
     * @return Class
     */
    @Override
    public Class<T> getEntityClass() {
        return entityClass;
    }


    public LambdaQueryChainWrapper<T> lambda() {
        return new LambdaQueryChainWrapper<>(
                baseDao,
                wrapperChildren.lambda()
        );
    }

}
